package org.zjh.view.login;


import org.zjh.controller.action.CancelJbtLis;
import org.zjh.controller.listener.FindPasswordJbtLis;
import org.zjh.controller.listener.LoginPanelLoginJbtLis;
import org.zjh.controller.listener.VerificationLis;
import org.zjh.utils.VerificationUtil;
import org.zjh.view.ui.GameUi;

import javax.swing.*;
import java.awt.*;

import static org.zjh.constant.ImageConstant.*;


/**
 * 登入面板
 */
public class LoginPanel extends JPanel {

    private JLabel verificationCodeLabel;
    private JTextField accountTextField;
    private JPasswordField passwordField;
    private JTextField verificationTextField;
    private JButton okJbt;
    private JButton cancelJbt;
    private JButton findPassword;


    public LoginPanel() {
        //调用初始化构造方法
        init();
        //调用监听器注册方法
        addListener();
    }

    /**
     * 初始化
     */
    private void init() {
        setLayout(null);

        //创建标签
        JLabel accountLabel = new JLabel("账号");
        JLabel passwordLabel = new JLabel("密码");
        JLabel verificationLabel = new JLabel("验证码");
        JLabel refreshLabel = new JLabel("点击验证码刷新");
        verificationCodeLabel = new JLabel(VerificationUtil.generateRandomVerificationCode());

        //设置标签大小
        accountLabel.setBounds(100, 50, 100, 50);
        passwordLabel.setBounds(100, 100, 100, 50);
        verificationLabel.setBounds(75, 150, 150, 50);
        verificationCodeLabel.setBounds(300, 150, 100, 50);
        refreshLabel.setBounds(300,165,150,100);

        //设置标签字体
        accountLabel.setFont(new Font("黑体", Font.PLAIN, 25));
        passwordLabel.setFont(new Font("黑体", Font.PLAIN, 25));
        verificationLabel.setFont(new Font("黑体", Font.PLAIN, 25));
        verificationCodeLabel.setFont(new Font("黑体", Font.PLAIN, 25));
        refreshLabel.setFont(new Font("黑体", Font.PLAIN, 15));

        //字体颜色加粗
        accountLabel.setForeground(Color.BLACK);
        passwordLabel.setForeground(Color.BLACK);
        verificationLabel.setForeground(Color.BLACK);
        verificationCodeLabel.setForeground(Color.BLACK);
        refreshLabel.setForeground(Color.BLACK);

        //创建文本框
        accountTextField = new JTextField();
        passwordField = new JPasswordField();
        verificationTextField = new JTextField();

        //为文本框设置属性
        accountTextField.setBounds(175, 60, 100, 25);
        passwordField.setBounds(175, 110, 100, 25);
        verificationTextField.setBounds(175, 160, 100, 25);

        //创建按钮
        okJbt = new JButton("登入");
        cancelJbt = new JButton(CANCEL_IMAGE);

        //为按钮设置属性
        okJbt.setBounds(50, 250, 100, 50);
        cancelJbt.setBounds(225, 250, 100, 50);
        findPassword = new JButton(FIND_PASSWORD);
        findPassword.setBounds(400, 250, 150, 50);

        //将创建的标签 文本框 按钮 添加到面板
        add(accountLabel);
        add(passwordLabel);
        add(verificationLabel);
        add(refreshLabel);
        add(verificationCodeLabel);
        add(accountTextField);
        add(passwordField);
        add(verificationTextField);
        add(okJbt);
        add(cancelJbt);
        add(findPassword);
    }

    /**
     * 添加监听器
     */
    private void addListener(){
        //验证码刷新监听器
        verificationCodeLabel.addMouseListener(new VerificationLis());
        //设置动作监听指令
        cancelJbt.setActionCommand("loginCancel");
        //返回按钮监听器
        cancelJbt.addActionListener(CancelJbtLis.getInstance());
        //确认按钮监听器
        okJbt.addMouseListener(new LoginPanelLoginJbtLis());
        //找回密码监听
        findPassword.addMouseListener(new FindPasswordJbtLis());
    }


    /**
     * 刷新页面
     */
    public void refresh() {
        accountTextField.setText("");
        passwordField.setText("");
        verificationTextField.setText("");
        //刷新页面时候验证码也刷新
        GameUi.loginFrame.getLoginPanel().getVerificationCodeLabel().setText(VerificationUtil.generateRandomVerificationCode());
    }




    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(LOGIN_PANEL_IMAGE.getImage(), 0, 0, LOGIN_PANEL_IMAGE.getIconWidth(), LOGIN_PANEL_IMAGE.getIconHeight(), null);
    }

    public JLabel getVerificationCodeLabel() {
        return verificationCodeLabel;
    }

    public JTextField getAccountTextField() {
        return accountTextField;
    }

    public JPasswordField getPasswordField() {
        return passwordField;
    }

    public JTextField getVerificationTextField() {
        return verificationTextField;
    }


}
